<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Notifications\PasswordNotification;
use Illuminate\Http\Request;

class PasswordController extends Controller
{
    //显示邮箱页面
    public function email()
    {
        return view('password.email');
    }

    //发送邮件
    public function send(Request $request)
    {
        //发送
        $user = User::where('email', $request->email)->first();
        \Notification::send($user, new PasswordNotification($user));
        session() ->flash('success','邮件发送成功,请前往邮件重置密码');
        return redirect() -> route('FindPasswordEmail');
    }

    //显示重置密码页面
    public function edit($token)
    {
        $user = $this->getUserToken($token);
        return view('password.edit', compact('user'));
    }

    //提交修改的密码
    public function update(Request $request)
    {
        $user = $this->getUserToken($request->token);
        $this->validate($request, [
            'password' => 'required|min:5|confirmed'
        ]);
        $user->password = bcrypt($request->password);
        $user->save();
        session()->flash('success', '密码修改成功！');
        return redirect()->route('login');
    }

    //获取user模型值
    public function getUserToken($token)
    {
        return User::where('email_token', $token)->first();
    }
}
